package com.szpx.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.szpx.entity.Film;
import com.szpx.service.FilmManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class FilmManageController {

    @Autowired
    private FilmManageService filmManageService;

    /**
     *  根据id查询影片的信息
     */

    @GetMapping("/selectFilmById/{id}")
    public String selectFilmById(@PathVariable("id") Integer id, Model model) {
        Film film = filmManageService.selectFilmById(id);
        if (film != null){
            model.addAttribute("film",film);
            return "chw/chw_FilmManageHtml/chw_FilmDetail";
        }else {
            return null;
        }
    }




    /**
     * 删除影片信息
     */
    @RequestMapping("/deleFilm/{id}")
    public String deleFilm(@PathVariable("id")Integer id){
        int num = filmManageService.deleFilm(id);
        if (num > 0)
            return "redirect:/flist"; //重定向到影片列表的请求
        else
            return null;
    }

    /**
     * 去影片修改页面
     */
    @GetMapping("/toUpdateFilm/{id}")
    public String toUpdateFilmPage(@PathVariable("id")Integer id
            ,Model model){
        Film film = filmManageService.selectFilmById(id);
        if (film != null){
            model.addAttribute("film",film); //将查询出来的信息 传到修改页面
            return "chw/chw_FilmManageHtml/chw_FilmUpdate";
        }else {
            return null;
        }
    }

    /**
     * 修改影片信息
     */
    @RequestMapping("/updateFilm")
    public String updateFilm(Film film){
        int num = filmManageService.updateFilm(film);
        if (num > 0)
            return "redirect:/flist";
        else
            return null;
    }

    /**
     * 添加影片
     */
    @RequestMapping("/addFilm")
    public String addFilm(Film film){
        int num = filmManageService.addFilm(film);
        if (num > 0)
            return "redirect:/flist";
        else
            return null;
    }

    /**
     * 跳转到添加影片界面
     */
    @RequestMapping("/toAddFilm")
    public String toAddFilm(HttpSession session){
        return "chw/chw_FilmManageHtml/chw_FilmAdd";
    }


    /**
     * 多条件分页查询所有影片
     */
    @GetMapping("/flist")
    public String getFlist(
            @RequestParam(defaultValue = "1") int pno,
            @RequestParam(defaultValue = "3") int pageSize,
            Film film,Model model){
        PageHelper.startPage(pno,pageSize);//开始分页
        List<Film> flist = filmManageService.getListByInfo(film);
        PageInfo<Film> page = new PageInfo<Film>(flist);
        model.addAttribute("flist",flist); //将查询到的数据放入model对象中
        model.addAttribute("page",page);
        model.addAttribute("fname",film.getFname());
        model.addAttribute("type",film.getType());
        model.addAttribute("pno",pno);
        return "chw/chw_FilmManageHtml/chw_FilmFlist";
    }

//    /**
//     * 访问模板逻辑视图model
//     * @param model
//     * @return
//     */
//    @GetMapping("/model")
//    public String model(Model model) {
//        return "model"; //逻辑视图
//    }
}
